<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="userDialogFormRef" :model="state.ruleForm" :rules="rules" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="项目名称" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入项目名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="价格" prop="price">
							<el-input v-model.number="state.ruleForm.price" placeholder="请输入价格" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="项目信息">
							<el-input v-model="state.ruleForm.introduce" type="textarea" placeholder="请输入项目信息"
								maxlength="150"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<script setup lang="ts" name="systemUserDialog">
import { reactive, ref } from 'vue';
import { programApi } from '/@/api/program/index';
import { ElMessage } from 'element-plus';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		id: '',
		name: '',
		price: null as number | null,
		introduce: '',
	},
	typeData: [] as any[],
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 表单校验规则
const rules = reactive({
	name: [
		{ required: true, message: '请输入项目名称', trigger: 'blur' },
	],
	price: [
		{ required: true, message: '请输入价格', trigger: 'blur' },
		{ type: 'number', message: '价格必须为数字', trigger: 'blur' },
	],
});

// 打开弹窗
const openDialog = (type: string, row: any) => {
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改用户';
		state.dialog.submitTxt = '修 改';
	} else {
		state.ruleForm = {
			id: '',
			name: '',
			price: null as number | null,
			introduce: '',

		};
		state.dialog.title = '新增房间';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		// userDialogFormRef.value?.resetFields();
	}
	state.dialog.type = type;

	state.dialog.isShowDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;

};

// 取消
const onCancel = () => {
	closeDialog();
};

// 提交
const onSubmit = () => {
	if (state.dialog.type === 'edit') {
		programApi().updateProgram(state.ruleForm).then((res) => {
			if (res.code === 1) {
				ElMessage.success('修改成功');
				closeDialog();
			}
		});
	} else {
		// 添加
		userDialogFormRef.value.validate((valid: boolean) => {
			if (valid) {
				programApi().saveProgram(state.ruleForm).then((res) => {
					if (res.code === 1) {
						ElMessage.success('添加成功');
						programApi().selectProgramPage().then((res) => {
							emit('refresh');
						});
					}
				});
				closeDialog();
				emit('refresh');
			} else {
				ElMessage.error('请检查输入项');
				return false;
			}
		});
	}

};

// 暴露变量
defineExpose({
	openDialog,
});
</script>